import qs from "qs"
import ManageConstant from "../consts/manageConstant";
import cookies from 'js-cookie'

class ManageService {

  constructor(_this) {
    this._this = _this;
    this._axios = _this.axios.create({
      baseURL: ManageConstant.DOMAIN_URL,
      timeout: 5000,
      headers: {
        ManageUserAccessToken: cookies.get('Token')
      }
    });
  }

  login(params) {
    this._axios.post(ManageConstant.LOGIN_PATH, params).then(res => {
      if (res.data.resultCode == 0) {
        cookies.set('Token', res.data.data[0].userAccessToken);
        cookies.set("UserId", res.data.data[0].userId);
        cookies.set("userName", res.data.data[0].userName);
        console.log("用户名：" + res.data.data[0].userName);
        console.log("token" + res.data.data[0].userAccessToken);
        console.log(res.data.data[0].userId);
        this._this.$router.push({path:'/index'});
        return {"isSuccess" : true};
        //登录成功跳转主页面（未完成）
        /*this._this.$router.push({path:'/main'})*/
        // console.log("调用router跳转")
         
      } else {
        return {"isSuccess" : false};
      }
    })
  }

  signUp(params) {
    this._axios.post(ManageConstant.SIGHUP_PATH, params).then(res => {
      if(res.data.resultCode == 0){
        cookies.set('Token', res.data.data[0].userAccessToken);
        cookies.set("UserId", res.data.data[0].userId);
        cookies.set("userName", res.data.data[0].userName);
        console.log("用户名：" + res.data.data[0].userName);
        console.log("token" + res.data.data[0].userAccessToken);
        console.log(res.data.data[0].userId)
        this._this.$Message.success('注册成功');
      }else{
        console.log(res.data.resultCode);
        this._this.$Message.error('用户已存在');
      }
    })
 
  }

  logout(){
  	console.log("登出");
  	cookies.remove('Token');
  	cookies.remove("UserId");
  	cookies.remove("userName") ;
    cookies.remove("password") ;
    this._this.$router.push({path:'/login'}) ;
  }

  getManageUserInfo(params, executeMethod){
    this._axios.get(ManageConstant.MANAGEUSER_INFO_GET, {params}).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

  editManageUserInfo(params, executeMethod){
    this._axios.post(ManageConstant.MANAGEUSER_INFO_EDIT, params).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }
}

export default ManageService;
